const API = require('../../utils/api')
const AUTH = require('../../utils/auth')
Page({
  data: {
    News: [],
    Token: [],
    isAuth: 1,
    Top: [{
      num: '0.00',
      name: '余额'
    }, {
      num: '0.00',
      name: '冻结'
    }, {
      num: '0',
      name: '积分'
    }, {
      num: '0',
      name: '成长值'
    }, ],
    // 订单
    GoodList: [{
      icon: 'balance-list-o',
      name: '待付款'
    }, {
      icon: 'logistics',
      name: '待发货'
    }, {
      icon: 'certificate',
      name: '待收货'
    }, {
      icon: 'flower-o',
      name: '待评价'
    }, {
      icon: 'after-sale',
      name: '售后'
    }, ],
    // 优惠买单
    YouList: [{
      icon: 'balance-list-o',
      name: '优惠买单'
    }, {
      icon: 'balance-list-o',
      name: '资金明细'
    }, {
      icon: 'bill-o',
      name: '申请发票'
    }, {
      icon: 'bill-o',
      name: '开票记录'
    }, {
      icon: 'label-o',
      name: '优惠券'
    }, {
      icon: 'calendar-o',
      name: '签到赚积分'
    }, ]
  },
  Login() {
    const _this = this;
    wx.getUserProfile({
      //授权语言
      lang: 'zh_CN',
      //授权描述
      desc: '申请用户授权',
      //成功返回
      success(res) {
        console.log('授权成功返回：', res)

        //调用更新用户头像和昵称的方法到
        _this.updateUserInfo(res.userInfo)
        //   const a=  AUTH.autoRegAndLogin()
        //   console.log(a);

      },
      //失败返回
      fail(error) {
        console.log('授权失败返回：', error)
      }
    })
  },
  async updateUserInfo(userInfo) {
    this.setData({
      News: userInfo
    })
    console.log(this.data.News);
    const isLogined = await AUTH.hasUserIsLogined()

    console.log(isLogined);
    if (isLogined) {
      // token
      const token = wx.getStorageSync('token')
      console.log(token);
      const {
        avatarUrl,
        city,
        gender,
        nickName: nick,
        province
      } = userInfo
      await API.updateUserInfoApi({
        city,
        nick,
        province,
        avatarUrl,
        gender,
        token
      })
      this.getUserDetail()
    }
  },
  // 余额和冻结跳转
  ZCJump(e) {
    console.log(e);
    let id = e.target.id
    if (id == 0 || id == 1) {
      wx.navigateTo({
        url: `/pages/ZiChan/ZiChan?id=${id}&name=我的资产`,

      })
    } else if (id == 2) {
      wx.navigateTo({
        url: `/pages/ZiChan/ZiChan?id=${id}&name=积分明细 &Name=可用积分`,

      })
    } else if (id == 3) {
      wx.navigateTo({
        url: `/pages/ZiChan/ZiChan?id=${id}&name=成长值明细&Name=当前成长值`,

      })
    }
    console.log(e.target.dataset.name);


  },
  KVjump(e) {
    if (e.target.dataset.name == '优惠券') {
      wx.switchTab({
        url: '/pages/coupon/coupon',
      })
      console.log(1111);
    } else {
      wx.navigateTo({
        url: `/pages/ZiChan/ZiChan`,

      })
    }
  },
  // 订单列表
  Danjump(e) {
    let Id = e.target.id * 1
    console.log(Id);

    wx.navigateTo({
      url: `/pages/DingDan/DingDan`,

    })
    console.log(e.target.id);
  },
  // CPS订单
  CPSDing() {
    wx.navigateTo({
      url: '/pages/CPSDing/CPSDing',
    })
  },
  // 回收订单
  HuiShou() {
    wx.navigateTo({
      url: '/pages/HuiShou/HuiShou',
    })
  },
  // 分销商
  FenXiao() {
    wx.navigateTo({
      url: '/pages/FenXiao/FenXiao',
    })
  },
  // 个人中心
  GeRen() {
    wx.navigateTo({
      url: '/pages/GeRen/GeRen',
    })
  },
  onShow: async function () {
    const isLogined = await AUTH.hasUserIsLogined()
    if (isLogined) {
      this.getUserDetail()
    }
  },

  async getUserDetail() {
    // 拿到token
    const token = wx.getStorageSync('token')
    const res = await API.getUserDetailApi(token)
    // console.log(res.base);
    this.setData({
      News: res.base
    })
    let Token = res.base
    const data1 = {}
    data1.userinfo = res.base
    if (res.base.avatarUrl && res.base.nick) {
      this.setData({
        isAuth: 2
      })
    } else {
      this.setData({
        isAuth: 1
      })
    }
    this.setData(data1)
  }
})